<?php
namespace frontend\controllers;

use common\models\ImageTypeModel;
use common\models\LianTypeModel;
use common\models\LianWebModel;
use common\models\NovelModel;
use common\models\NovelTypeModel;
use common\models\SetAuthorModel;
use frontend\models\SmsCodeModel;
use Yii;
use yii\data\Pagination; //分页
use frontend\base\BaseController;
use frontend\utils\GetUrl;
use frontend\utils\UrlUtil;
use frontend\utils\CurlUtil;

/**
 * Collection controller
 */
class JianpanController extends BaseController
{
    public $name = '';
    public $type = '';
    public static $novel_type='';

    /**
     * 欢迎页
     * @return string
     */
    public function actionDemo()
    {
        $str = file_get_contents('http://www.janpn.com/');
        $par = '/<li><a.*href="(.*)">(.*)<\/a><\/li>/Uis';
        preg_match($par, $str, $arr);
        var_dump($arr);die;
        if(!empty($arr[1])){
            $par = '/.*src="(.*)".*alt="(.*)".*<p>(.*)<\/p>.*">(.*)<\/a>/Uis';
            preg_match_all($par,$arr[1],$arr2);
            var_dump($arr2);exit;
        }
        echo '测试成功,欢迎使用!!!'.'<br><br><br>';
        echo "['collection-star'=>'传参数name，采集name对应的网站信息,type=1一级(类型)  2下级']
                <a href='/collection/collection-star?name=shubao&type=1' target='_blank' style='text-decoration:none;'>   去采集吧 >></a>".'<br>';
    }

    //键盘  小说类型
    public function actionGetType()
    {
        $str = file_get_contents('http://www.janpn.com/');
        $par = '/<li><a.*href="(.*)">(.*)<\/a><\/li>/Uis';
        preg_match_all($par, $str, $arr);
        $res = [];
        if(!empty($arr)){
            unset($arr[0]);
            $num = count($arr[1]);
            foreach ($arr as $key => $value) {
                foreach ($value as $k => $val) {
                    if($key == 1){
                        $name = 'url';
                    }elseif($key == 2){
                        $name = 'title';
                    }
                    $res[$k][$name] = str_replace('"','',$val);
                }
            }
        }
        if(!empty($res)){
            foreach ($res as $k => $val) {
                $data[] = ['title' => $val['title'], 'source' => 1, 'url' => $val['url'],'time'=>time()];
            }
        }
        $model = NovelTypeModel::tableName();
        $attr = ['title', 'source', 'url','time'];
        $flag = Yii::$app->db->createCommand()->batchInsert($model, $attr, $data)->execute();
        echo $flag;
    }

    public function actionGetNovel()
    {
        ini_set("max_execution_time", "72000");
        ini_set('user_agent','Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322)');
        $type_list = NovelTypeModel::find()->select(['id','url','title','page'])->where(['source'=>1])->asArray()->all();
        if(!empty($type_list)){
            foreach ($type_list as $k => $val) {
                for ($i=$val['page'];;$i++){
                    $str = @file_get_contents($val['url'].'list-'.($k+1).'-'.$i.'.html');
                    if(!$str){
                        break;
                    }
                    $par = '/<a.*title="(.*)".*class="shop".*href="(.*)">.*<div.*class="shop-info">.*<\/div>.*<div.*class="shop-info">(.*)<\/div>.*<div.*class="shop-info">.*<span.*class="glyphicon.*glyphicon-bullhorn">.*<\/a>/Uis';
                    preg_match_all($par, $str, $arr);
                    if(!empty($arr[0])){
                        $sta = NovelTypeModel::updateAll(['page'=>$i+1],['id'=>$val['id']]);
                        unset($arr[0]);
                        $data = $res = [];
                        foreach ($arr as $key => $value) {
                            foreach ($value as $num => $v) {
                                if($key == 1){
                                    $name = 'title';
                                }elseif($key == 2){
                                    $name = 'url';
                                }elseif($key == 3){
                                    $name = 'author';
                                }
                                $res[$num][$name] = trim(str_replace('"','',$v));
                            }
                        }
                        if(!empty($res)){
                            foreach ($res as $x => $y) {
                                $count = NovelModel::find()->where(['title' => $y['title'],'source'=>1,'type'=>$val['id']])->count();
                                if($count == 0) {
                                    $data[] = ['title' => $y['title'], 'source' => 1, 'url' => $y['url'], 'author'=>$y['author'], 'type'=>$val['id']];
                                }
                            }
                            if(!empty($data)){
                                $model = NovelModel::tableName();
                                $attr = ['title', 'source', 'url','author','type'];
                                $status = Yii::$app->db->createCommand()->batchInsert($model, $attr, $data)->execute();
                            }
                        }
                    }else{
                        break;
                    }
                }
                continue;
            }
        }
    }

}